package com.tsd.base.service.impl;

import com.tsd.base.dao.BasePrintDesignMapper;
import com.tsd.base.entity.BasePrintDesign;
import com.tsd.base.entity.BasePrintDesignExt;
import com.tsd.base.service.BasePrintDesignService;
import com.tsd.common.vo.BatchDeleteLogicVo;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.HlpException;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.entity.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 描述：BasePrintDesign 服务实现层
 *
 * @author Hillpool
 * @date 2022/10/13 10:54:22
 */
@Service
public class BasePrintDesignServiceImpl extends BaseServiceImpl implements BasePrintDesignService {

    @Resource
    private BasePrintDesignMapper basePrintDesignMapper;

    @Override
    public BasePrintDesign getBasePrintDesignById(Long id) throws Exception {
        return basePrintDesignMapper.selectByPrimaryKey(id);
    }

    @Override
    public BasePrintDesignExt queryBySid(String sid) throws Exception {
        return basePrintDesignMapper.selectBySid(sid);
    }

    @Override
    public void deleteBasePrintDesigns(List<String> list, SysUser opUser) throws Exception {
        if (HlpUtils.isEmptyList(list)) {
            return;
        }
        BatchDeleteLogicVo logicVo = new BatchDeleteLogicVo(opUser);
        logicVo.setSidList(list);
        basePrintDesignMapper.deleteLogicBySidList(logicVo);
    }

    @Override
    public void saveBasePrintDesign(BasePrintDesignExt record, SysUser opUser) throws Exception {
        this.checkDuplicated(record);
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getType(), "归属功能不能为空");
            super.checkEmpty(record.getCode(), "编码不能为空");
            super.checkEmpty(record.getName(), "名称不能为空");
            if (HlpUtils.isEmpty(record.getSn())) {
                int maxSn = basePrintDesignMapper.selectMaxSn(record);
                record.setSn(maxSn + 1);
            }
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            basePrintDesignMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            basePrintDesignMapper.updateByPrimaryKeySelective(record);
        }
    }

    public void checkDuplicated(BasePrintDesignExt designExt) throws Exception {
        BasePrintDesignExt params = new BasePrintDesignExt();
        params.setId(designExt.getId());
        params.setType(designExt.getType());
        params.setCode(designExt.getCode());
        Integer num = basePrintDesignMapper.checkDuplicatedObj(params);
        if (num != null) {
            throw new HlpException("已存在编码：" + designExt.getCode());
        }
        params.setCode(null);
        params.setName(designExt.getName());
        num = basePrintDesignMapper.checkDuplicatedObj(params);
        if (num != null) {
            throw new HlpException("已存在名称：" + designExt.getName());
        }
    }

    @Override
    public List<BasePrintDesignExt> findBasePrintDesigns(Map<String, Object> params, PageBean page) throws Exception {
        return basePrintDesignMapper.selectByPage(params, page);
    }

    @Override
    public List<BasePrintDesignExt> findBasePrintDesigns(Map<String, Object> params) throws Exception {
        return basePrintDesignMapper.selectByParams(params);
    }
}